<?php

namespace Home\Controller;

class RbacController extends CommonController
{
    public function index()
    {
        $this->display();
    }
    // 添加用户
    public function addUser()
    {
        $role = M('role')->select();
        $this->assign('role', $role);
        $this->display();
    }

    public function addUserHandle()
    {
        $user = M('user');
        $userinfo = M('userinfo');
        $data = array(
            'user_name' => I('post.user_name'),
            'password' => md5(I('post.password')),
            'lock' => I('post.lock'),
            'logtime' => time(),
            'logip' => get_client_ip(),
        );
        $role = array();
        if (IS_POST) {
            if ($uid = $user->add($data)) {
                foreach ($_POST['role_id'] as $key => $value) {
                    $role[] = array(
                        'role_id' => $value,
                        'user_id' => $uid,
                    );
                }
                $infoData = array(
                    'true_name' => I('post.true_name'),
                    'phone_num' => I('post.phone_num'),
                    'id_num' => I('post.id_num'),
                    'company' => I('post.company'),
                    'depart' => I('post.depart'),
                    'position' => I('post.position'),
                    'part' => I('post.part'),
                    'work_id' => I('work_id'),
                    'remark' => I('post.remark'),
                    'uid' => $uid,
                  );
                if (M('role_user')->addAll($role) && $userinfo->add($infoData)) {
                    $this->success('添加成功', U('Home/Rbac/userList'));
                }
            } else {
                $this->error('添加失败', U('Home/Rbac/addUser'));
            }
        }
    }

    // 添加角色
    public function addRole()
    {
        $this->display();
    }

    public function addRoleHandle()
    {
        $role = M('role');
        if (IS_POST) {
            if ($role->add($_POST)) {
                $this->success('添加成功', U('Home/Rbac/roleList'));
            } else {
                $this->error('添加失败', U('Home/Rbac/addRole'));
            }
        }
    }

    // 添加节点
    public function addNode()
    {
        $pid = I('pid', 0, 'intval');
        $level = I('level', 1, 'intval');
        switch ($level) {
            case 1:
                $type = '应用';
                break;
            case 2:
                $type = '控制器';
                break;
            case 3:
                $type = '动态方法';
                break;
        }

        $this->assign('type', $type);
        $this->assign('pid', $pid);
        $this->assign('level', $level);
        $this->display();
    }

    public function addNodeHandle()
    {
        $node = M('node');
        if (IS_POST) {
            if ($node->add($_POST)) {
                $this->success('添加成功', U('Home/Rbac/nodeList'));
            } else {
                $this->error('添加失败', U('Home/Rbac/addNode'));
            }
        }
    }

    // 添加权限
    public function addAccess()
    {
        $rid = I('rid', 1, 'intval');
        $field = array('id','name','remark','pid');
        $node = M('node')->field($field)->order('sort')->select();
        $access = M('access')->where(array('role_id' => $rid))->getField('node_id', true);
        $node = node_merge($node, $access);
        $this->assign('node', $node);
        $this->assign('rid', $rid);
        $this->display();
    }

    public function addAccessHandle()
    {
        $rid = I('rid', 1, 'intval');
        $db = M('access');
        $db->where(array('role_id' => $rid))->delete();
        $data = array();
        foreach ($_POST['access'] as $key => $value) {
            $tmp = explode('_', $value);
            $data[] = array(
                    'role_id' => $rid,
                    'node_id' => $tmp[0],
                    'level' => $tmp[1], );
        }
        $result = $db->addAll($data);
        if ($result) {
            $this->success('添加成功', U('Home/Rbac/roleList'));
        } else {
            $this->error('没有权限被添加', U('Home/Rbac/roleList'));
        }
    }

    // 用户列表
    public function userList()
    {
        $this->fpage('UserRelation', 10);
        $this->display();
    }

    // 角色列表
    public function roleList()
    {
        $this->assign('role', M('role')->order('id')->select());
        $this->display();
    }

    // 节点列表
    public function nodeList()
    {
        $this->assign('node', M('node')->order('sort')->select());
        $this->display();
    }

    // 冻结用户
    public function lockMod()
    {
        $id = I('id');
        $user = M('user');
        if (I('status') == 0) {
            $user->where(array('id' => $id))->setField('lock', 0);
        } elseif (I('status') == 1) {
            $user->where(array('id' => $id))->setField('lock', 1);
        }
        $this->redirect('Home/Rbac/userList');
    }

    private function fpage($table, $listRows)
    {
        $table = D($table);
        $count = $table->relation(true)->count();
        $fpage = new \Think\Page($count, $listRows);
        $fpage->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;
              第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $fpage->setConfig('first', '首页');
        $fpage->setConfig('prev', '上一页');
        $fpage->setConfig('next', '下一页');
        $fpage->setConfig('last', '末页');
        $fpage->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
        $fpage->rollPage = 5;
        $pageResult = $table->limit($fpage->firstRow.','.$fpage->listRows)
                                ->relation(true)->select();
        $fpage->lastSuffix = false;
        $show = $fpage->show();
        $this->assign('count', $count);
        $this->assign('listRows', $fpage->listRows);
        $this->assign('relation', $pageResult);
        $this->assign('show', $show);
    }
}
